/**
 * 将天、小时和分钟转换为总秒数
 * @param days 天
 * @param hours  小时
 * @param minutes 分钟
 * @returns
 */
export const daysHoursMinutesToSeconds = ({ days, hours, minutes }) => {
  return days * 86400 + hours * 3600 + minutes * 60
}

/**
 * 将秒数转换为天、小时和分钟
 * @param seconds 秒
 * @returns
 */
export const secondsToDaysHoursMinutes = (seconds) => {
  const days = Math.floor(seconds / 86400)
  const hours = Math.floor((seconds % 86400) / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const remainingSeconds = seconds % 60
  return { days, hours, minutes, seconds: remainingSeconds }
}

// 获取今日的开始和结束日期
function getToday() {
  const today = new Date();
  return {
    startDate: formatDate(today),
    endDate: formatDate(today),
  };
}

// 获取昨天的开始和结束日期
function getYesterday() {
  const yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  return {
    startDate: formatDate(yesterday),
    endDate: formatDate(yesterday),
  };
}

// 获取近一周的开始和结束日期
function getLastWeek() {
  const today = new Date();
  const lastWeekStart = new Date(today);
  lastWeekStart.setDate(today.getDate() - 6); // 近一周是 7 天，包括今天
  return {
    startDate: formatDate(lastWeekStart),
    endDate: formatDate(today),
  };
}

// 获取近一个月的开始和结束日期
function getLastMonth() {
  const today = new Date();
  const lastMonthStart = new Date(today);
  lastMonthStart.setMonth(today.getMonth() - 1);
  return {
    startDate: formatDate(lastMonthStart),
    endDate: formatDate(today),
  };
}

// 获取近一年的开始和结束日期
function getLastYear() {
  const today = new Date();
  const lastYearStart = new Date(today);
  lastYearStart.setFullYear(today.getFullYear() - 1);
  return {
    startDate: formatDate(lastYearStart),
    endDate: formatDate(today),
  };
}


export function getStartAndEndDate(range) {
  let result = {}
  switch (range) {
    case 'today':
      result = getToday()
      break;
    case 'yesterday':
      result = getYesterday()
      break;
    case 'lastWeek':
      result = getLastWeek()
      break;
    case 'lastMonth':
      result = getLastMonth()
      break;
    case 'lastYear':
      result = getLastYear()
      break;
    default:
      console.error('Invalid range option');
      return;
  }

  return result
}

function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}
